
import commonMixin from '@/mixins/commonMixin'
import { formatDate } from '@/utils/index'
import { getCategoryAllList } from '@/api/goods/category';
import { getSupplierAllList } from '@/api/goods/supplier/index';
import { listQuotationSchemeAll } from '@/api/goods/quote/index';
import { getGoodsSpecGroupAllList } from '@/api/goods/goodsSpecGroup';
import { listInvoicingMethod } from '@/api/goods/invoicingMethod';
import { listContractType } from '@/api/goods/contractType';
import { getClueSourceAllList } from '@/api/goods/clueSource';
import { handleTree } from '@/utils/ruoyi';
import { listArea } from '@/api/goods/area';
import { getUserNotPermission } from '@/api/system/user';
const modelMixin = {
    mixins: [commonMixin],
    data() {
        return {
            dicts: [], // 字典数据
            form: {},
            loading: false,
            supplierList: [],//供应商/客户
            quotationSchemeList: [],//报价方案
            goodsSpecGroupList: [],//规格组
            defaultSpecGroupList: [],//扁平规格组
            areaList: [],//区域
            invoicingMethodList: [],//开票方式
            contractTypeList: [],//合同类型
            clueSource: [],//线索来源
            assistList: [],//辅助人员
        };
    },
    computed: {
    },
    created() {
    },
    methods: {
        /**
         * @编辑
         * @param {*} id 
         */
        edit(row) {
            this.open = true;
            this.getDetail(row);
        },
        /**
         * @获详情
         * @param {*} type 
         */
        getDetail(row) {
            this.loading = true
            this.funcObj.get(row.id).then(res => {
                this.loading = false
                if (res.code == 200) {
                    this.form = res.data || {};
                    if (typeof (this.detailAfter) === 'function') {
                        this.detailAfter()
                    }
                } else {
                    this.$modal.msgError(res.msg)
                }
            }).catch(err => {
                this.loading = false
                console.log('err :>> ', err);
            })
        },
        /**
         * @提交
         * @param {*} close 
         */
        submitForm(close = true) {
            this.$refs.form.validate(valid => {
                console.log('valid :>> ', valid);
                if (valid) {
                    typeof (this.classifyForm) === 'function' && this.classifyForm()

                    this.loading = true
                    if (this.form.id) {
                        this.funcObj.update(this.form).then(res => {
                            this.loading = false
                            if (res.code == 200) {
                                this.$modal.msgSuccess('操作成功')
                                if (close) {
                                    this.open = false;
                                }
                                this.$emit('ok',this.form);
                            } else {
                                this.$modal.msgError(res.msg)
                            }
                        }).catch(err => {
                            this.loading = false
                            console.log('err :>> ', err);
                        })
                    } else {
                        this.funcObj.add(this.form).then(res => {
                            this.loading = false
                            if (res.code == 200) {
                                this.$modal.msgSuccess('新增成功')
                                if (close) {
                                    this.open = false;
                                }
                                this.$emit('ok',this.form);
                            } else {
                                this.$modal.msgError(res.msg)
                            }
                        }).catch(err => {
                            console.log('err :>> ', err);
                            this.loading = false
                        })
                    }

                }
            });
        },
        cancel() {
            this.open = false;
            this.$emit('cancel');
        },
        /**
         * @获取分类
         */
        getCategoryAllList() {
            getCategoryAllList().then(res => {
                this.categoryList = res.data
            }).catch(err => {
                console.log(err)
            });
        },
        /**
         * 获取所有客户/供应商 
         * @param {String | Number} type 
         * type 2:供应商 1:客户
         */
        getSupplierAllList(type) {
            return new Promise((resolve, reject) => {
                let params = {
                    type: type
                }
                getSupplierAllList(params).then(res => {
                    this.supplierList = res
                    resolve(res)
                }).catch(err => {
                    console.log(err)
                });
            })
        },
        /**
         * @获取所有报价方案
         */
        listQuotationSchemeAll() {
            return new Promise((resolve, reject) => {
                listQuotationSchemeAll().then(res => {
                    this.quotationSchemeList = res
                    resolve(res)
                }).catch(err => {
                    console.log(err)
                });
            })
        },

        // 获取规格组
        getGoodsSpecGroupAllList() {
            getGoodsSpecGroupAllList().then(res => {
                this.goodsSpecGroupList = [
                    {
                        id: 0,
                        specGroupName: '全部',
                        children: handleTree(res)
                    }
                ]
                this.defaultSpecGroupList = res
            }).catch(err => {
                console.log(err)
            });
        },
        // 获取区域
        getArea() {
            listArea().then(res => {
                this.areaList = res.rows
            }).catch(err => {
                console.log(err)
            });
        },
        // 获取开票方式
        listInvoicingMethod() {
            listInvoicingMethod().then(res => {
                this.invoicingMethodList = res.rows
            }).catch(err => {
                console.log(err)
            });
        },
        // 获取合同类型
        listContractType() {
            listContractType().then(res => {
                this.contractTypeList = res.rows
            }).catch(err => {
                console.log(err)
            });
        },
        // 获取跟单来源
        
    getClueSourceAllList() {
        getClueSourceAllList().then((res) => {
            console.log('666',666);
          this.clueSource = res;
        });
      },
      // 后台用户
      getUserNotPermission() {
        getUserNotPermission()
          .then((res) => {
            this.assistList = res;
          })
          .catch((err) => {
            console.log(err);
          });
      },
        handlePeriodicNumChange(row) {
            let date = row.beginDate
            date = Date.parse(new Date(date)) / 1000; // 转换成时间戳，返回值是ms，除1000后转化成秒
            date += (86400) * row.periodicNum; // 一天是86400秒
            let newDate = new Date(parseInt(date) * 1000); // 把时间戳转换成日期
            //这步就是给所需变化的值赋值
            row.endDate = formatDate(newDate)
        },
        handleEndDateChange(row) {
            let date1 = Date.parse(new Date(row.beginDate))
            let date2 = Date.parse(new Date(row.endDate))
            row.periodicNum = parseInt(Math.abs(date2 - date1) / 1000 / 86400)
        },
    },
};

export default modelMixin;